#!/usr/bin/lua
https = require("ssl.https")
http = require("socket.http")
ltn12 = require("ltn12")
require("lua-xwrt.addon.uci")
util = require("lua-xwrt.xwrt.util")
require("lua-xwrt.addon.string")

function do_uci(t)
	local cmd = t.command
	local file = t.file
	local config = t.config
	local option = t.option or t.list
	local value = t.value
	if t.option then
		local stat
		if file and config and option and value then
			stat = uci[cmd](file,config,option,value)
		elseif file and config and option then
			stat = uci[cmd](file,config,option)
			if cmd == set then
				print(string.format("%s(%s.%s=%s) %s",cmd,file, config, option, value, tostring(stat)))
			else
				
		elseif file and config then
			stat = uci[cmd](file,config)
			print(string.format("%s %s.%s %s",cmd,file, config, tostring(stat)))
		end
		print(string.format("%s(%s.%s.%s=%s) %s",cmd,file, config, option, value, tostring(stat)))
	else
		for y, u in ipairs(t) do
			for j, n in ipairs(u) do
				local opt = n.option or n.list
				local stat = uci[cmd](file,config,opt, n.value)
				print(string.format("%s(%s.%s.%s=%s) %s",cmd, file, config, opt, n.value, tostring(stat)))
			end
		end
	end
	uci.save(file)
	uci.commit(file)
end

function do_file(t)
	local cmd = t.command
	local file = t.filename
	local data = t.value
	print(cmd, file)
end

function do_shell(t)
	local cmd = t.command
	local shell = io.popen(cmd)
	print("<pre>")
	for line in shell:lines() do
		print(line)
	end
	print("</pre>")
end

function processData(o)
	if o.title then
		print(o.title)
	end
	for i,t in ipairs(o) do
		if type(t) == "table" then
			for k, v in pairs(t) do
				if k == "uci" then
					do_uci(v)
				elseif k == "file" then
					do_file(v)
				elseif k == "shell" then
					do_shel(v)
--				else
--					print(k,v)
				end
			end
--		else
--			print(i,t)
		end
	end
end

--local url = "https://wms.intra-urbalink.com/services/simple.php?module=wireless_ms&action=agent-config&ap=ap-0028&key=y2aeapkwn6exv2a3szm5"
--local body = ""
--local res, code, headers, status = https.request(url, body)
--print(res)
--print(code)
--print(headers)
--print(status)
local device = string.escape(uci.get("webif","general","device_name"))
local apname = string.escape((uci.get("urbalink","general","apname") or "Unknow"))
local key = string.escape((uci.get("urbalink","general","key") or "NoKey"))
local url = uci.get("urbalink","general", "url")
url = url....string.format("?apname=%s&key=%s&device=%s",apname,key,device)
--url = "http://dev.voiceip.com.ar/services/ap-config.lua?"..string.format("apname=%s&key=%s&device=%s",apname,key,device)
print(url)
--url = "http://dev.voiceip.com.ar/services/ap-config.lua"
local res, code, headers, status = http.request(url, body)
--print(code)
--print(headers)

--for k,v in pairs(headers) do
--	print("",k,v)
--end
print(status)
print(res)
if code == 200 then
	if res then 
		local data = io.open("/var/urbalink.dat","w")
		data:write(res)
		data:close()
		entry = processData
		dofile("/var/urbalink.dat")
	end
end
